//
//  WBStatusCell.swift
//  新浪微博
//
//  Created by 名品导购网MPLife.com on 2017/11/30.
//  Copyright © 2017年 sweet. All rights reserved.
//

import UIKit
import SDWebImage

@objc protocol WBRetweetedCellDelegate:NSObjectProtocol {
    //协议方法
    //设置可选协议方法
    //协议需要设置Objc
    //方法需要 @objc optional
    @objc optional func RetweetedCellDidSelectedUrlString(cell:WBStatusRetweetedCell,urlStr:String)
}


class WBStatusRetweetedCell: UITableViewCell {
    weak var delegate:WBRetweetedCellDelegate?
    
    private lazy var iconView:UIImageView = UIImageView()
    private lazy var memberIconView:UIImageView = UIImageView()
    private lazy var nameLabel:UILabel = UILabel()
    private lazy var vipIconViewImage:UIImageView = UIImageView()
    private lazy var timelabel:UILabel = UILabel()
    private lazy var sourceLabel:UILabel = UILabel()
//    private lazy var statusLabel:UILabel = UILabel()
    private lazy var statusLabel:FFLabel = FFLabel()
    private lazy var bottomView:UIView = UIView()
    private lazy var reweetedButton:UIButton = UIButton(type: .custom)
    private lazy var commenButton:UIButton = UIButton(type: .custom)
    private lazy var likeButton:UIButton = UIButton(type: .custom)
    private lazy var imageCenterView:WBStatusPictureView = WBStatusPictureView()
    private lazy var retButton:UIButton = UIButton(type: .custom)
//    private lazy var retLabel:UILabel = UILabel()
    private lazy var retLabel:FFLabel = FFLabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super .init(style: style, reuseIdentifier: reuseIdentifier)
        //离屏渲染
        //importantPoint
        //如果检测到cell的性能已经很好，不需要在离屏渲染 和栅格化 会额外消耗CPU 会很耗电
        self.layer.drawsAsynchronously = true
        //栅格化-异步绘制后会生成一张独立的图像 在cell上滚动的时候，本质上滚动的是这张图片
        //cell优化，尽量减少图层的数量 相当于就只有一层
        //停止滚动之后 可以接收监听
        self.layer.shouldRasterize = true
        //使用栅格化 必须制定分辨率，否则字体会不清晰
        self.layer.rasterizationScale = UIScreen.main.scale
        
        setUpUi()
        
        statusLabel.delegate = self
        retLabel.delegate = self
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("错误信息")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
    var viewModel : WBStatusViewModel? {
        didSet{
            guard let user : WBUser = viewModel?.status.user,let urlString = user.profile_image_url else {
                return
            }
            iconView .cz_setImage(urlStrin: urlString, placeHoderImage: UIImage(named: "avatar_default_big"),isAvatar: true)
            imageCenterView.viewModel = viewModel
            nameLabel .text = user .screen_name
//            timelabel .text = viewModel?.status.created_at
            timelabel .text = viewModel?.status.createDate?.cz_dateDescription
            statusLabel.attributedText = viewModel?.statusAttrText
            sourceLabel .text = viewModel?.status.source
            memberIconView .image = viewModel?.memberIcon
            vipIconViewImage.image = viewModel?.vipIcon
            retLabel.attributedText = viewModel?.retweetedAttrText
            reweetedButton .setTitle(viewModel?.reportStr, for: .normal)
            commenButton .setTitle(viewModel?.commentStr, for: .normal)
            likeButton .setTitle(viewModel?.attitudeStr, for: .normal)
            guard let urlHeight:CGFloat = viewModel?.automaticHeight else {
                return
            }
            let imageHeight:CGFloat = viewModel?.pictureViewSize.height ?? 0
            guard let nameString = nameLabel.text else {
                return
            }
            guard let retText = viewModel?.retweetedAttrText else {
                    return
            }
            let retTextHeight:CGFloat = getLabelAttributedTextHeight(lableStr: retText, font: UIFont.systemFont(ofSize: 14), width: ScreenWidth-22)
            self.contentView.frame = CGRect(x: 0, y: 0, width: ScreenWidth, height: viewModel?.retweetedCellHeight ?? 0)
            nameLabel.frame = CGRect(x: nameLabel.frame.origin.x, y: nameLabel.frame.origin.y, width: getLabWidth(labelStr: nameString, font: UIFont .systemFont(ofSize: 15), height: 20), height: 20)
            memberIconView.frame = CGRect(x: nameLabel.frame.origin.x + nameLabel.frame.size.width + 5, y: nameLabel.frame.origin.y+1, width: 15, height: 15)
            timelabel.frame = CGRect(x: timelabel.frame.origin.x, y: timelabel.frame.origin.y, width: 100, height: timelabel.frame.size.height)
            sourceLabel.frame = CGRect(x:timelabel.frame.origin.x+timelabel.frame.size.width , y: sourceLabel.frame.origin.y, width: self.contentView.frame.size.width-(timelabel.frame.origin.x+timelabel.frame.size.width)-10, height: sourceLabel.frame.size.height)
            statusLabel.frame = CGRect(x: statusLabel.frame.origin.x, y: statusLabel.frame.origin.y, width: statusLabel.frame.size.width, height: urlHeight)
            
            retButton.frame = CGRect(x: 0, y: statusLabel.frame.origin.y+statusLabel.frame.size.height, width: ScreenWidth, height: 12+retTextHeight+imageHeight+12)
            retLabel.frame = CGRect(x: 11, y: 12, width: ScreenWidth-22, height: retTextHeight)
            imageCenterView.frame = CGRect(x: retLabel.frame.origin.x, y: retLabel.frame.origin.y + retLabel.frame.size.height, width: ScreenWidth-22, height: imageHeight)
            
           bottomView.frame = CGRect(x: retButton.frame.origin.x, y: retButton.frame.origin.y +
            retButton.frame.size.height, width: ScreenWidth, height: 35)
        }
    }
}
//协议方法
extension WBStatusRetweetedCell:FFLabelDelegate{
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        delegate?.RetweetedCellDidSelectedUrlString?(cell: self, urlStr: text)
    }
}
extension WBStatusRetweetedCell {
    private func setUpUi(){
        let topView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 12))
        topView .backgroundColor = UIColor .cz_color(withHex: 0xf2f2f2)
        self .contentView .addSubview(topView)
        
        //会员头像
        iconView.frame = CGRect(x: 11, y: 12 + 12, width: 34, height: 34)
        self.contentView .addSubview(iconView)
        //会员图标
        vipIconViewImage.frame = CGRect(x: iconView.frame.origin.x + iconView.frame.size.width-10, y: iconView.frame.origin.y + iconView.frame.size.height - 10, width: 12, height: 12)
        self.contentView .addSubview(vipIconViewImage)
        //姓名
        nameLabel.frame = CGRect(x: iconView.frame.origin.x+iconView.frame.size.width + 11, y: iconView.frame.origin.y + 3, width: 60, height: 20)
        nameLabel .textColor = UIColor .cz_color(withHex: 0xfc3e00)
        nameLabel .font = UIFont .systemFont(ofSize: 15)
        nameLabel .text = "甜甜"
        self.contentView .addSubview(nameLabel)
        //认证图标
        memberIconView.frame = CGRect(x: nameLabel.frame.origin.x + nameLabel.frame.size.width + 5, y: nameLabel.frame.origin.y-2, width: 15, height: 15)
        self.contentView .addSubview(memberIconView)
        //时间
        timelabel.frame = CGRect(x: nameLabel.frame.origin.x, y: nameLabel.frame.origin.y + nameLabel.frame.size.height, width: 60, height: 20)
        timelabel .textColor = UIColor .cz_color(withHex: 0xff6c00)
        timelabel .font = UIFont .systemFont(ofSize: 10)
        timelabel .text = "刚刚"
        self.contentView .addSubview(timelabel)
        //微博正文
        sourceLabel.frame = CGRect(x: timelabel.frame.origin.x + timelabel.frame.size.width + 3, y: timelabel.frame.origin.y, width: 60, height: 20)
        sourceLabel .textColor = UIColor .cz_color(withHex: 0x828282)
        sourceLabel .font = UIFont .systemFont(ofSize: 10)
        sourceLabel .text = "微博"
        self.contentView .addSubview(sourceLabel)
        
        let  rightArrow = UIButton(type: .custom)
        rightArrow.frame = CGRect(x: ScreenWidth-25, y: 20, width: 15, height: 10)
        
        rightArrow .setBackgroundImage(UIImage(named: "navigationbar_arrow_down"), for: .normal)
        rightArrow .addTarget(self, action: #selector(rightClickRetweed), for: .touchUpInside)
        self.contentView .addSubview(rightArrow)
        
        statusLabel.frame = CGRect(x: iconView.frame.origin.x, y: iconView.frame.origin.y + iconView.frame.size.height + 12, width: ScreenWidth-22, height: 80)
        statusLabel .numberOfLines = 0
        statusLabel .font = UIFont .systemFont(ofSize: 15)
        self.contentView .addSubview(statusLabel)
        
        retButton.frame = CGRect(x: 0, y: statusLabel.frame.origin.y+statusLabel.frame.size.height, width: ScreenWidth, height: 210)
        retButton.backgroundColor = UIColor .cz_color(withHex: 0xe3e3e3)

        self.contentView.addSubview(retButton)
        
        retLabel.frame = CGRect(x: 11, y: 12, width: ScreenWidth-22, height: 20)
        retLabel.textColor = UIColor .darkGray
        retLabel.font = UIFont .systemFont(ofSize: 12)
        retLabel.numberOfLines = 0
        retLabel.text = "测试测试"
        retButton.addSubview(retLabel)
        
        imageCenterView.frame = CGRect(x: retLabel.frame.origin.x, y: retLabel.frame.origin.y+retLabel.frame.size.height, width: retLabel.frame.size.width, height: 180)
        retButton .addSubview(imageCenterView)
        
        
//        bottomView.frame = CGRect(x: 0, y: retweedButton.frame.origin.y + retweedButton.frame.size.height, width: ScreenWidth, height: 35)
        bottomView.backgroundColor = UIColor .white
        self.contentView .addSubview(bottomView)//+10
        let width = (ScreenWidth-2)/3
        reweetedButton.frame = CGRect(x:0 , y: 0, width: width, height: 35)
        reweetedButton.tag = 100
        reweetedButton .setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        reweetedButton .setImage(UIImage(named: "timeline_icon_retweet"), for: .normal)
        reweetedButton .setTitle("转发", for: .normal)
        reweetedButton .setTitleColor(UIColor.black, for: .normal)
        reweetedButton.titleLabel?.font = UIFont .systemFont(ofSize: 12)
        reweetedButton .titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0)
        reweetedButton.addTarget(self, action: #selector(buttonClickRetweed), for: .touchUpInside)
        bottomView.addSubview(reweetedButton)
        
        let imageView1 = UIImageView(frame: CGRect(x: reweetedButton.frame.origin.x + reweetedButton.frame.size.width, y: reweetedButton.frame.origin.y, width: 1, height: 35))
        imageView1.image = UIImage(named: "timeline_card_bottom_line")
        bottomView.addSubview(imageView1)
        
        commenButton.frame = CGRect(x:imageView1.frame.origin.x+imageView1.frame.size.width , y: reweetedButton.frame.origin.y, width: width, height: 35)
        commenButton.tag = 101
        commenButton .setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        commenButton .setImage(UIImage(named: "timeline_icon_comment"), for: .normal)
        commenButton .setTitle("评论", for: .normal)
        commenButton .setTitleColor(UIColor.black, for: .normal)
        commenButton.titleLabel?.font = UIFont .systemFont(ofSize: 12)
        commenButton .titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0)
        commenButton.addTarget(self, action: #selector(buttonClickRetweed), for: .touchUpInside)
        bottomView.addSubview(commenButton)
        
        let imageView2 = UIImageView(frame: CGRect(x: commenButton.frame.origin.x + commenButton.frame.size.width, y: commenButton.frame.origin.y, width: 1, height: 35))
        imageView2.image = UIImage(named: "timeline_card_bottom_line")
        bottomView.addSubview(imageView2)
        
        likeButton.frame = CGRect(x:imageView2.frame.origin.x+imageView2.frame.size.width , y: commenButton.frame.origin.y, width: width, height: 35)
        likeButton.tag = 103
        likeButton .setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), for: .normal)
        likeButton .setImage(UIImage(named: "timeline_icon_unlike"), for: .normal)
        likeButton .setImage(UIImage(named: "timeline_icon_like"), for: .selected)
        likeButton .setTitle("点赞", for: .normal)
        likeButton .setTitleColor(UIColor.black, for: .normal)
        likeButton .titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0)
        likeButton.titleLabel?.font = UIFont .systemFont(ofSize: 12)
        likeButton.addTarget(self, action: #selector(buttonClickRetweed), for: .touchUpInside)
        bottomView.addSubview(likeButton)
    }
    @objc private func rightClickRetweed(){
        print("点击右边")
    }
    @objc private func buttonClickRetweed(btn:UIButton){
        if btn .tag == 100 {
            print("转发")
        }else if btn.tag == 101 {
            print("评论")
        }else{
            print("点赞")
        }
    }
}











